#include "cmpd_rt_object.hpp"

bool CmpdRTobject::FindNearestIntersection(Ray& intersectRay, double& dist)
{
	bool retVal=false;
	double minDist=FLT_MAX;
	
	for (unsigned int i=0; i<objectsList.size(); i++)
	{			
		if ((objectsList[i])->Intersect(intersectRay,dist)) 
			if (dist<minDist) 
			{
				minDist=dist;
				CmpdRTobject * temp = dynamic_cast<CmpdRTobject*>(objectsList[i]);
				
				if (temp==NULL) nearestObject=objectsList[i];
				else nearestObject = temp->nearestObject;
				
				retVal=true;
			}
	}
	dist=minDist;
	return retVal;
}

bool CmpdRTobject::Intersect(Ray& intersectRay, double& dist)
{
	bool retVal = FindNearestIntersection(intersectRay, dist);
	return retVal;
}

Color CmpdRTobject::Trace(Ray& viewRay, Point3D lightPos, double& dist)
{
	Color retColor;
	if (nearestObject!=NULL) retColor = nearestObject->Trace(viewRay, lightPos, dist);
	return retColor;
}
